﻿using Storage.Core.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Storage.Mapping.BucketImages;
using Storage.Core.Caching;
using Storage.Core.Encyption;
using Storage.Entities;

namespace Storage.Services.BucketImages
{
    public class BucketImagesService : IBucketImagesService
    {
        private const string MODEL_KEY = "Storage.services.bucket_images_{0}";
        private const int MODEL_DAYS = 15 * 24 * 60;
        private IRepository<Entities.BucketImage> _bucketImagesRepository;
        private ICacheManager _cacheManager;

        public BucketImagesService(IRepository<Entities.BucketImage> bucketImagesRepository,
            ICacheManager cacheManager)
        {
            this._bucketImagesRepository = bucketImagesRepository;
            this._cacheManager = cacheManager;
        }


        /// <summary>
        /// 通过url路径获取图片
        /// </summary>
        /// <param name="relativeUrl"></param>
        /// <returns></returns>
        public BucketImagesMapping getBacketImage(string relativeUrl)
        {
            return _cacheManager.get<BucketImagesMapping>(String.Format(MODEL_KEY, EncryptorHelper.GetMD5(relativeUrl)), MODEL_DAYS, () =>
               {
                   return _bucketImagesRepository.Table.Where(o => o.RelativeUrl == relativeUrl)
                    .Select(item => new BucketImagesMapping()
                    {
                        Id = item.Id,
                        RelativeUrl = item.RelativeUrl,
                        FileName = item.FileName,
                        IOPath = item.IOPath,
                        Width = item.Width,
                        Height = item.Height,
                        BucketId=item.BucketId
                    }).FirstOrDefault();
               });
        }

        /// <summary>
        /// 插入文件
        /// </summary>
        /// <param name="entity"></param>
        public void insertImage(Entities.BucketImage entity)
        {
            _bucketImagesRepository.insert(entity);
            _cacheManager.set(String.Format(MODEL_KEY, EncryptorHelper.GetMD5(entity.RelativeUrl)), new BucketImagesMapping()
            {
                Id = entity.Id,
                FileName = entity.FileName,
                IOPath = entity.IOPath,
                Width = entity.Width,
                RelativeUrl = entity.RelativeUrl,
                Height = entity.Height,
                BucketId=entity.BucketId
            }, MODEL_DAYS);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void deleteImage(int id)
        {
            var item = _bucketImagesRepository.getById(id);
            if (item == null)
                return;
            _bucketImagesRepository.delete(item);
            _cacheManager.remove(String.Format(MODEL_KEY, EncryptorHelper.GetMD5(item.RelativeUrl)));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sha1"></param>
        /// <returns></returns>
        public BucketImagesMapping getBySHA1(string sha1)
        {
            return _bucketImagesRepository.Table.Select(item => new BucketImagesMapping()
            {
                Id = item.Id,
                RelativeUrl = item.RelativeUrl,
                IOPath = item.IOPath,
                SHA1 = item.SHA1
            }).FirstOrDefault(o => o.SHA1 == sha1);
        }
         
    }
}
